function login(username){
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: "action=login&username="+username,
		async: false
	});
}

function logout(){
	loggedIn = false;
	refresh();
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: "action=logout",
		async: false
	});
}

function sendShipLocations(){
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: "action=addShipLocations&coordinateList="+myBoard.getShipLocationJsonString() + "&shipList=" + myBoard.getShipsJsonString(),
		async: false,
		success: function (response){
		}
	});
}


function createNewGame(){
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: "action=newGame",
		async: false,
		success: function (succesful) {
			if(succesful == "true"){
				gameBoardOpen = true;
				setToPlayingState();
			}
			else{
				$("#errorArea").show("fast");
	            document.getElementById("errorArea").innerHTML = "You can play only one game at a time";
			}
		}
	});
}

function updateLobby(){
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: "action=updateLobby",
		async: true,
		success: function (response) {
			document.getElementById("lobby").innerHTML = response;
			if(loggedIn){
				updateLobby();
			}
		} 
	});
}

function joinGame(player1){
	var d = "action=joinGame&player1="+player1;
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: d,
		async: false,
		success: function (succesful) {
			if(succesful == "true"){
				setToPlayingState();
			}
			else{
				$("#errorArea").show("fast");
	            document.getElementById("errorArea").innerHTML = "You can play only one game at a time";
			}
		}
	});
}

function leaveGame(){
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: "action=leaveGame",
		async: false,
		success: function (succesful) {
			if(succesful == "true"){
				refresh();
			}
			else{}
		}
	});
}

function checkShot(){
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: "action=checkShot",
		async: true,
		success: function (result) {
			var response = result.split(";");
			var row = response[0];
		    var col = response[1];
		    var type = response[2];
			updateBoard("2", row, col, type);
			checkShot();
		}
	});
}



function getOtherPlayerStatus(){
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: "action=otherPlayerStatus",
		async: true,
		success: function (result) {
			if(result == "ready"){
				document.getElementById("errorArea").innerHTML = "Other player is ready!";
				$("#errorArea").show("fast").delay(3000).hide("fast");
			}
			else if(result == "yourTurn"){
				document.getElementById("errorArea").innerHTML = "Your turn";
				$("#errorArea").show("fast");
			}
			else if(result == "opponentsTurn"){
				document.getElementById("errorArea").innerHTML = "Opponent's turn";
				$("#errorArea").show("fast");
			}
			else if(result == "joined"){
				document.getElementById("errorArea").innerHTML = "Other player joined your game";
				$("#errorArea").show("fast").delay(3000).hide("fast");
			}
			else if(result == "logOutContinue"){
				document.getElementById("errorArea").innerHTML = "Opponent left, but you may wait new one";
				$("#errorArea").show("fast");
			}
			else if(result == "logOutOver"){
				document.getElementById("errorArea").innerHTML = "Opponent left, you WIN";
				$("#errorArea").show("fast");
				$('#startButton').attr('onclick', 'refresh();');
			}
			else if(result == "waiting"){
				document.getElementById("errorArea").innerHTML = "Waiting for other player...";
				$("#errorArea").show("fast");			
			}
			else if(result == "waitForOther"){
				document.getElementById("errorArea").innerHTML = "Other player needs to join your game";
				$("#errorArea").show("fast").delay(3000).hide("fast");
				$("#startButton").attr("onclick", "playGame()");
				document.getElementById("startButton").innerHTML = startButtonName;
			}
			else if(result == "positioningOkey"){
				serverOkey = true;
			}
			else if(result == "invalidPositioning"){
				serverOkey = false;
			}
			else if(result == "loser"){
				getReplayData();
				document.getElementById("errorArea").innerHTML = "Game over, you LOST";
				$("#errorArea").show("fast");
				$("#startButton").attr("onclick", "refresh()");
				$("#opponentTable .shootable").click(function(){
					document.getElementById("errorArea").innerHTML = "There is nothing to do anymore, you LOST!";
					$("#errorArea").show("fast");
			});
				
				saveReplay();
				gameOn = false;
			}
			else if(result == "winner"){
				getReplayData();
				document.getElementById("errorArea").innerHTML = "Game over, you WIN";
				$("#errorArea").show("fast");
				$("#startButton").attr("onclick", "refresh()");
				$("#opponentTable .shootable").click(function(){
					document.getElementById("errorArea").innerHTML = "Camoon man, you WON! Stop shooting";
					$("#errorArea").show("fast");
			});
				
				saveReplay();
				gameOn = false;
				
			}
			if(loggedIn){
			getOtherPlayerStatus();
			}
		}
	});
}

function shootIfYourTurn(id){
	row = id.charAt(1);
    col = id.charAt(2);
	d = "action=shootIfYourTurn&row=" + row +"&col="+ col;
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: d,
		async: false,
		success: function (result) {
			if(result == "false"){
				document.getElementById("errorArea").innerHTML = "It's not your turn yet";
				$("#errorArea").show("fast");
			}
			else if(result == "otherLeft"){
				document.getElementById("errorArea").innerHTML = "It seems that your opponent gave up, you WIN";
				$("#errorArea").show("fast");
			}
			else if(result == "oldSpot"){
				document.getElementById("errorArea").innerHTML = "Seems that you have already shot here...shoot again";
				$("#errorArea").show("fast");
			}
			else{
				updateBoard("1", row, col, result);
			}
			
		}
	});
}

function gameIsOn(){
	d = "action=gameOn";
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: d,
		async: false,
		success: function (result) {
			if(result == "true"){
				$('#lobbyButton').show('fast');
				$('#lobbyButton').attr('onclick', 'lobbyToggle();');
				$('#startButton').attr('onclick', 'leaveGame();');
				document.getElementById("startButton").innerHTML = "Leave";
				setTimeout("$('#lobby').hide('fast');", 400);
				$('#middlePart').delay(600).show('fast');
				loggedIn = true;
				gameBoardOpen = true;
			}
		}
	});
}



function updateScores(){
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: "action=updateScores",
		async: true,
		success: function (response) {
			var cont = response;
			var content = cont.split(",");
			var table = '<table class="Score-table">\
			<tbody>\
			<th><b>ID</b></th>\
			<th><b>Username</b></th>\
			<th><b>Wins</b></th>\
			<th><b>Losses</b></th>\
			<th><b>Difference</b></th>';
			for (var i=0; i<content.length-1; i += 4){
				table += '<tr>\
				<td class=Score-table-second>'+((i/4)+1)+'</td>\
				<td class=Score-table-second>'+content[i]+'</td>\
				<td class=Score-table-second>'+content[i+1]+'</td>\
				<td class=Score-table-second>'+content[i+2]+'</td>\
				<td class=Score-table-second>'+content[i+3]+'</td>\
				</tr>';
			}
			table += '</tbody></table>';
		    document.getElementById("highScore").innerHTML = table;
			if(loggedIn){
				updateScores();
			}
		} 
	});
}

function getReplayData(){
	getP1Shooting();
	getP2Shooting();
	getStarter();
	ships = myBoard.getShipLocationJsonString();
}

function getGameId(){
	d = "action=getGameId";
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: d,
		async: false,
		success: function (result) {
			gameId = result;
		}
	});
}

function getP1Name(){
	d = "action=getP1Name";
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: d,
		async: false,
		success: function (result) {
			p1Name = result;
		}
	});
}

function getP2Name(){
	d = "action=getP2Name";
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: d,
		async: false,
		success: function (result) {
			p2Name = result;
		}
	});
}

function getP1Shooting(){
	d = "action=getP1Shooting&gameId="+gameId;
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: d,
		async: false,
		success: function (result) {
			p1Shooting= result;
		}
	});
}

function getP2Shooting(){
	d = "action=getP2Shooting&gameId="+gameId;
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: d,
		async: false,
		success: function (result) {
			p2Shooting= result;
					}
	});
}

function getStarter(){
	d = "action=getStarter&gameId="+gameId;
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: d,
		async: false,
		success: function (result) {
			starter = result;
		}
	});
}

function getFBLoginLink(){
	d = "action=fbLogin";
	$.ajax({
		url: "Warboat",
		type: "POST",
		data: d,
		async: false,
		success: function (result) {
			var l = result.split(";");
			console.log("<a href='https://www.facebook.com/dialog/oauth?client_id=" +  l[2] + "&amp;redirect_uri=" + l[1] + "&amp;state=" + l[0] + "'>Logi sisse FB-ga</a>");
			document.write("<a href='https://www.facebook.com/dialog/oauth?client_id=" +  l[2] + "&amp;redirect_uri=" + l[1] + "&amp;state=" + l[0] + "'>Logi sisse FB-ga<br /><img src='images/facebook-button.gif' alt='Facebook' id='fbimage' /></a>");
		}
	});
}